package lolnotepad;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.sql.*;
import java.util.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

/**
 *
 * @author havlicekdom@gmail.com
 */
public class LoLNotepad implements ActionListener {

    public void build() {       //function creating main frame
        try (Connection conn = DriverManager.getConnection("jdbc:mysql://sql4.freemysqlhosting.net:3306/sql419186", "sql419186", "jS3%gQ4*");) {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            final ArrayList<String> champions = new ArrayList();
            final ArrayList<String> roles = new ArrayList();
            final ArrayList<String> leagues = new ArrayList();
            ResultSet rs1, rs2, rs3;
            try (Statement st1 = conn.createStatement()) {
                rs1 = st1.executeQuery("SELECT * FROM champs ORDER BY name ASC");
                while (rs1.next()) {
                    champions.add(rs1.getString("name"));
                }
            }
            rs1.close();
            try (Statement st2 = conn.createStatement()) {
                rs2 = st2.executeQuery("SELECT * FROM roles ORDER BY id_role ASC");
                while (rs2.next()) {
                    roles.add(rs2.getString("role"));
                }
            }
            rs2.close();
            try (Statement st3 = conn.createStatement()) {
                rs3 = st3.executeQuery("SELECT * FROM leagues ORDER BY id_league ASC");
                while (rs3.next()) {
                    leagues.add(rs3.getString("league"));
                }
            }
            rs3.close();
            final MainFrame frame = new MainFrame("LoL Notepad");
            JLabel label = new JLabel("Choose your action:");
            final JBackgroundPanel bgPanel = new JBackgroundPanel();
            JButton button1 = new JButton("Write new note");
            JButton button2 = new JButton("Read existing notes");
            button1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    frame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                    frame.remove(bgPanel);
                    Writer writer = new Writer(frame, champions, roles, leagues);
                    frame.setCursor(Cursor.getDefaultCursor());
                }
            });
            button2.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    frame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                    frame.remove(bgPanel);
                    Reader reader = new Reader(frame, champions, roles, leagues);
                    frame.setCursor(Cursor.getDefaultCursor());
                }
            });
            button1.setLayout(null);
            button2.setLayout(null);
            label.setForeground(Color.white);
            bgPanel.add(label);
            bgPanel.add(button1);
            bgPanel.add(button2);
            bgPanel.setPreferredSize(new Dimension(640, 480));
            bgPanel.setBorder(new EmptyBorder(150, 0, 0, 0));
            frame.add(bgPanel);
            JMenuBar menuBar = new Menu(frame, bgPanel, champions, roles, leagues);
            frame.setJMenuBar(menuBar);
            frame.pack();
            conn.close();
        } catch (Exception e) {
        }
        }

    

    public static void main(final String[] args) throws IOException {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (UnsupportedLookAndFeelException | ClassNotFoundException | InstantiationException | IllegalAccessException e) {
            // handle exception
        }
        new LoLNotepad().build();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}

class MainFrame extends JFrame {

    public MainFrame(String title) {
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                int answer = JOptionPane.showConfirmDialog(null, "You want to quit?", "Quit", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
                if (answer == JOptionPane.YES_OPTION) {
                    System.exit(0);
                }
            }
        });
        setTitle(title);
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        setIconImage(Toolkit.getDefaultToolkit().getImage("./img/LoLNotepad.png"));
        setResizable(false);
        setVisible(true);
        setSize(657, 523);
        setLocationRelativeTo(null);
    }
}

class JBackgroundPanel extends JPanel {     //class creating background image

    private BufferedImage img;

    public JBackgroundPanel() {
        try {
            img = ImageIO.read(new File("./img/bg.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
    }
}
